package com.lepu.app.fun.my;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.app.utils.LoginUtil;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.eyzhs.app.R;
import com.lepu.app.fun.mother_circle.adapter.MessageListAdapter;
import com.lepu.app.fun.my.bean.BeanMessage;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

public class MyMessageActivity extends BaseFragmentActivity implements
        OnTopbarNewLeftLayoutListener, AsyncRequest {

	private static final String REQUEST_MSG = "request_msg";
	
    private ListView mDataListView = null;
    private MessageListAdapter mDataAdapter;
    private ArrayList<BeanMessage> mDataArrayList = new ArrayList<BeanMessage>();
    
    public int mBoard = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_sys_message_activity);

        init();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        
        BeanMessage.updateNewMessageRead(LoginUtil.getUserId(), 4);
    }

    /**
     * 添加页面动画，需要重写这个方法，
     */
    @Override
    public void onBackPressed() {
    	BeanMessage.updateNewMessageRead(LoginUtil.getUserId(), mBoard);
        finish(true);
    }
    
    @Override
    public void onTopbarLeftLayoutSelected() {
    	BeanMessage.updateNewMessageRead(LoginUtil.getUserId(), mBoard);
        finish(true);
    }

    public void showHideNoDataLayout(boolean isShow) {
        LinearLayout noDataLayout = (LinearLayout) findViewById(R.id.noDataLayout);
        if (noDataLayout != null) {
            if (isShow) {
                noDataLayout.setVisibility(View.VISIBLE);
            } else {
                noDataLayout.setVisibility(View.GONE);
            }
        }
    }

    public void showFirstLoadDataLayout(boolean isShow) {
        LinearLayout firstLoadDataLayout = (LinearLayout) findViewById(R.id.firstLoadDataLayout);
        if (firstLoadDataLayout != null) {
            if (isShow) {
                firstLoadDataLayout.setVisibility(View.VISIBLE);
            } else {
                firstLoadDataLayout.setVisibility(View.GONE);
            }
        }
    }
    
    /**
     * 初始化方法
     */
    private void init() {
        // topbar
        CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
        topbar.setTopbarTitle(R.string.my_main_message);
        topbar.setonTopbarNewLeftLayoutListener(this);

        Bundle data = getIntent().getExtras();
        if (data != null && data.size() > 0) {
            mBoard = data.getInt("type");
        }
        
        mDataListView = (ListView) findViewById(R.id.mListview);
        
        refreshView();
        
        requestAlarm();
    }

	/**
	 * 请求消息
	 */
	private void requestAlarm() {
		String url = Setting.getMassegeDataUrl();

		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject params = new JSONObject();
		try {
			params.put("DeviceID", Setting.DEVICEID);
			params.put("ApplicationID", Setting.APPLICATIONID);
		    params.put("UserID", LoginUtil.getUserId());
			params.put("LoginToken", LoginUtil.getLoginToken());
			params.put("MessageType", mBoard);
		} catch (Exception e) {
			
		}
		map.put("ht", params);
		ApiClient.http_post_main(url, map, null, this, REQUEST_MSG, false);
	}
	
	@Override
	public void RequestComplete(Object object, Object data) {
		ArrayList<BeanMessage> beanList = BeanMessage.parseJsonData((String) data);
        if(beanList != null && beanList.size() > 0){
            for(BeanMessage item : beanList)
            {
                item.Board = mBoard;
                item.IsRead = 0;
                item.LoginUserID = LoginUtil.getUserId();

                if(!BeanMessage.getMsgIsExist(LoginUtil.getUserId(), mBoard, item.IMID)){
                    BeanMessage.insertMyMessage(item);
                }
            }
        }

		refreshView();
	}

	@Override
	public void RequestError(Object arg0, int arg1, String arg2) {
		
	}

	
	private void refreshView(){
		//将系统消息添加到该用户下
		if(LoginUtil.checkIsLogin()){
			ArrayList<BeanMessage> arrayList = BeanMessage.getMyMessage("-1", mBoard, "MessageTime desc");
            if(arrayList != null && arrayList.size() > 0){
                for(int i=0; i<arrayList.size(); i++){
                    BeanMessage item = arrayList.get(i);
                    if(!BeanMessage.getMsgIsExist(LoginUtil.getUserId(), mBoard, item.IMID)){
                        item.LoginUserID = LoginUtil.getUserId();
                        BeanMessage.insertMyMessage(item);
                    }
                }
            }
		}
		
		mDataArrayList = BeanMessage.getMyMessage(LoginUtil.getUserId(), mBoard, "MessageTime desc");
		if(mDataArrayList == null || mDataArrayList.size() == 0){
			showHideNoDataLayout(true);
			return;
		}else{
			showHideNoDataLayout(false);
			mDataAdapter = new MessageListAdapter(MyMessageActivity.this, mDataArrayList);
			mDataListView.setAdapter(mDataAdapter);
		}
	}
    
}
